<?php

namespace App\Http\Controllers\Api;

use App\Exports\BalanceHistoryExport;
use App\Http\Controllers\Controller;
use App\Rep\BalanceHistoryRep;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class BalanceHistory extends Controller
{
    protected $rep;

    public function __construct(Request $request)
    {
        $this->rep = new BalanceHistoryRep();
    }
    //流水账管理
    public function getBalanceHistoryList(Request $request){
        $params = $request->all();
        $export = $request->get('export')??0;
        if($export == 1){
            try{
                $params['order_name'] = '交易明细列表';
                $filename = date('ymdhis').'.xlsx';
                $export = new BalanceHistoryExport($params);
                return Excel::download($export,$filename,\Maatwebsite\Excel\Excel::XLSX,[
                    'Content-Type'=> 'application/octet-stream',
                    'Content-Disposition'=> 'attachment; filename='.$filename,
                    'Content-Transfer-Encoding'=> 'binary',
                ]);
            }catch(\Exception $exception){
                return re(0,[],$exception->getMessage());
            }
        }
        $data = $this->rep->getBalanceHistoryListRep($params);
        return re(0,$data,'success');
    }
}
